import derbyos
import zmq
from derbyos import DerbyXML
from derbyos import Stopwatch
from derbyos import NSOde
import wx
import wxDerbyOS
# need to use wx events to move updates from stopwatch to main event loop        
from wxDerbyOS import UpdateOnMainThread, Stopwatcher


class NSOdeJamTimer(wx.Frame):
    def __init__(self, nsode):
        self.nsode = nsode
        super(NSOdeJamTimer, self).__init__(None, title="Jam Timer",
                                            size=(400, -1))
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)

        self.startButton = wx.Button(self, -1, "Start")
        self.Bind(wx.EVT_BUTTON, self.OnStart, self.startButton)
        self.stopButton = wx.Button(self, -1, "Stop")
        self.Bind(wx.EVT_BUTTON, self.OnStop, self.stopButton)
        self.timeOutButton = wx.Button(self, -1, "Time Out")
        self.Bind(wx.EVT_BUTTON, self.OnTimeOut, self.timeOutButton)
        self.sizer2.Add(self.startButton, 1, wx.EXPAND)
        self.sizer2.Add(self.stopButton, 1, wx.EXPAND)
        self.sizer2.Add(self.timeOutButton, 1, wx.EXPAND)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.text = wx.StaticText(self, 1, "")
        self.sizer.Add(self.text, 0, wx.EXPAND)
        self.sizer.Add(self.sizer2, 1, wx.EXPAND)

        #Layout sizers
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)

        self.curJam = -1
        self.curPeriod = -1
        self.curTimeout = -1
        self.curLineup = -1
        self.curIntermission = -1
        self.jamTimer = Stopwatch(0.2, 120, False)
        self.periodTimer = Stopwatch(0.2, 30 * 60, False)
        self.lineupTimer = Stopwatch(0.2, 30)
        self.timeoutTimer = Stopwatch(0.2, 60 * 60)
        self.intermissionTimer = Stopwatch(0.2, 12 * 60 * 60)
        mainUpdate = UpdateOnMainThread(self, self.UpdateTimer)
        self.watchers = [Stopwatcher(self.jamTimer, "jam", self.nsode, mainUpdate),
                         Stopwatcher(self.periodTimer, "period", self.nsode, mainUpdate),
                         Stopwatcher(self.lineupTimer, "lineup", self.nsode, mainUpdate),
                         Stopwatcher(self.timeoutTimer, "timeout", self.nsode, mainUpdate),
                         Stopwatcher(self.intermissionTimer, "intermission", self.nsode, mainUpdate)]
        self.UpdateSummary()

    def UpdateTimer(self, timer):
        if timer == self.jamTimer:
            self.UpdateJamTimer()
        elif timer == self.periodTimer:
            self.UpdatePeriodTimer()
        elif timer == self.lineupTimer:
            self.UpdateLineupTimer()
        elif timer == self.timeoutTimer:
            self.UpdateTimeoutTimer()
        elif timer == self.intermissionTimer:
            self.UpdateIntermissionTimer()
        self.UpdateSummary()

    # these are all in response to our button actions
    def OnStart(self, event):
        self.timeoutTimer.StopTimer()
        self.jamTimer.StartTimer()
        self.periodTimer.ResumeTimer()
        self.intermissionTimer.StopTimer()
        self.lineupTimer.StopTimer()
        self.nsode.StopTimeout()
        self.nsode.StartJam()

    def OnStop(self, event):
        if self.jamTimer.IsRunning():
            self.EndJam()
            self.nsode.StopJam()

    def OnTimeOut(self, event):
        self.jamTimer.StopTimer()
        self.periodTimer.StopTimer()
        self.lineupTimer.StopTimer()
        self.timeoutTimer.StartTimer()
        self.nsode.StopJam()
        self.nsode.StartTimeout()

    def UpdateSummary(self):
        if self.intermissionTimer.IsRunning() and not self.periodTimer.IsRunning():
            self.text.SetLabelText("Intermission:" + self.intermissionTimer.FormattedTime())
        elif self.timeoutTimer.IsRunning():
            self.text.SetLabelText(
                "P:" + self.periodTimer.FormattedTime() + "\tTO:" + self.timeoutTimer.FormattedTime())
        elif self.lineupTimer.IsRunning():
            self.text.SetLabelText("P:" + self.periodTimer.FormattedTime() + "\tLU:" + self.lineupTimer.FormattedTime())
        else:
            self.text.SetLabelText("P:" + self.periodTimer.FormattedTime() + "\tJ:" + self.jamTimer.FormattedTime())

    def EndJam(self):
        self.jamTimer.StopTimer()
        if self.periodTimer.elapsedTime >= self.periodTimer.maximumTime:
            self.EndPeriod()
        else:
            self.lineupTimer.StartTimer()

    def EndPeriod(self):
        print "End Period"
        self.intermissionTimer.StartTimer()

    def UpdateJamTimer(self):
        #print "Jam",self.jamTimer.FormattedTime()
        if self.jamTimer.elapsedTime >= self.jamTimer.maximumTime:
            print "End of jam"
            self.EndJam()

    def UpdatePeriodTimer(self):
        #print "Period",self.periodTimer.FormattedTime()
        pass

    def UpdateLineupTimer(self):
        #print "Lineup",self.lineupTimer.FormattedTime()
        pass

    def UpdateTimeoutTimer(self):
        #print "Timeout",self.timeoutTimer.FormattedTime()
        pass

    def UpdateIntermissionTimer(self):
        #print "Intermission",self.intermissionTimer.FormattedTime()
        if self.intermissionTimer.elapsedTime >= self.intermissionTimer.maximumTime:
            print "End Intermission"


if __name__ == '__main__':
    import derbyos.Defaults

    optionsParser = derbyos.Defaults.OptionsParser()
    optionsParser.set_defaults(timer=True)  # by default, we send timer information
    wxDerbyOS.testWX("Jam Timer", NSOdeJamTimer, optionsParser=optionsParser)
